import {
    SearchMouseEnter,
    SearchGetFocus,
    SearchLostFocus,
    ChangeList,
    NextTips,
    SearchMouseLeave,
    CloseSpin
} from './actionTypes';
import Axios from "axios";
import { fromJS } from "immutable";

// 改变数组
const changeList = (data) => {
    return {
        type: ChangeList,
        data: fromJS(data),
        totalPage: Math.ceil(data.length / 10)
    }
};

// 获得焦点
export const inputOnfocus = () => {
    return {
        type: SearchGetFocus,
    }
};
// 失去焦点
export const inputOnBlur = (value) => {
    return {
        type: SearchLostFocus,
    }
};
// 热门搜索鼠标移入
export const searchMouseEnter = () => {
    return {
        type: SearchMouseEnter
    }
};
// 热门搜索鼠标移除
export const searchMouseLeave = () => {
    return {
        type: SearchMouseLeave
    }
};
// 换一批
export const nextTips = () => {
    return {
        type: NextTips
    }
};
// 关闭动画
export const closeSpin = () => {
    return {
        type: CloseSpin
    }
};
// 获得数组
export const getList = (dispatch) => {
    Axios.get('http://localhost:3000/data').then(res => {
        if(res.status === 200){
            const data = res.data;
            const action = changeList(data);
            dispatch(action);
        }else {
            console.log(res.statusText);
        }
    }).catch(e => {
        console.log(e);
    })

};
